<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Customer</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>登录日志</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">渠道类型</label>
                            <div class="layui-input-inline">
                                <div id="serverGroup"></div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">选择区服:</label>
                            <div class="layui-input-inline">
                                <select name="gameArea" class="layui-select" >
                                    <option v-for="(item,index) in services" :value="item.server_id" :selected="index==0">{{item.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">游戏账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="memberName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nick" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roleId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">IP</label>
                            <div class="layui-input-inline">
                                <input type="text" name="ip" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">设备号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="deviceCode" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">记录时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="createDateBegin" id="createTime0"
                                       placeholder="开始时间" autocomplete="off" class="layui-input" />
                                <input type="text" name="createDateEnd" id="createTime1"
                                       placeholder="截止时间" autocomplete="off" class="layui-input" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">地图名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="mapName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i>搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="export" auth="roleLogin:export"> 导出 </button>
    </div>
</script>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script src="/xm-select-v1.2.4/xm-select.js"></script>

<script>
    var maps = getEnum('map')
    var serverGroup = getEnum('serverGroup')

    new Vue({
        el: "#app",
        data: {
            services: JSON.parse(localStorage.getItem('services')),
        },
        mounted: function () {
            var _this = this;
            layui.use(['form', 'table'], function () {
                 $ = layui.jquery,
                    form = layui.form,
                    table = layui.table,
                    laydate=layui.laydate;

                laydate.render({
                    elem: '#createTime0',
                    value: getYmd(0)
                });
                laydate.render({
                    elem: '#createTime1',
                    value: getYmd(1)
                })
                var dataSg = [];
                for(var k in serverGroup){
                    var v = serverGroup[k];
                    dataSg.push({
                        name:v.name,
                        value:v.id,
                    })
                }

                xmSelect.render({
                    el: '#serverGroup',
                    name:"serverGroup",
                    language: 'zn',
                    data: dataSg,
                    on: function(data){
                        // 选择器值发生变化时触发
                        console.log('DATA:', data);
                        var selectedValues = data.arr.map(item => item.value);

                        // 选择服务器
                        handleServerGroupChange(selectedValues);
                    }
                })

                function handleServerGroupChange(selectedValues) {
                    // 在这里编写您想要执行的逻辑
                    if (selectedValues.length === 0) {
                        // 如果 selectedValues 为空数组,则不发送请求
                        console.log('No server groups selected.');
                        return;
                    }
                    console.log('Selected server groups:', selectedValues);
                    var url = '/admin/server/serverList';
                    var params = { groups: selectedValues };
                    var paramsString = $.param(params);
                    $.get(url + '?' + paramsString, function(res) {
                        console.log('res:', res.data);
                        _this.$set(_this, 'services', res.data);
                        _this.$nextTick(function() {
                            console.log('进来了么:');
                            form.render();
                        });
                    });
                }
                var reload = function(){
                    table.render({
                        width:2356,
                        where:form.val('table-search'),
                        elem: '#currentTableId',
                        url: '/admin/roleLogin/paginate',
                        defaultToolbar: ['filter', 'print' ],
                        toolbar: '#toolbarDemo',
                        cols: [[
                            {type: "checkbox", width: 50},
                            {field: 'logDate', title:'记录时间',width:160 },
                            {field: 'serverId', title:'服务器ID' },
                            {field: 'memberName', title:'游戏账号' },
                            {field: 'roleId', title:'角色ID'},
                            {field: 'nick', title:'角色名'},
                            {field: 'type', title:'游戏状态',templet: function(row) {
                                    // 根据状态值返回对应的文本
                                    return row.type === 0 ? '登录' : '离线';
                                }},
                            {field: 'level', width:100,title:'角色等级'},
                            {field: 'jobName', title:'角色职业'},
                            {field: 'mapId', width:80, title:'地图ID' },
                            {field: 'mapName', title:'地图名' },
                            {field: 'mapTypeName', title:'场景类型' },
                            {field: 'platform', title:'平台'},

                            {field: 'deviceCode',width:320, title:'设备号'},
                            {field: 'deviceId',width:320, title:'mac',templet:function(row){
                                    // 使用正则表达式匹配 IP 地址
                                    const regex = /Mac:(.*)\|/;
                                    const matches = row.deviceId.match(regex);
                                    // 提取匹配到的 IP 地址
                                    const ipAddress = matches ? matches[1] : '';
                                    return ipAddress;
                                }},
                            {field: 'deviceId',width:320, title:'Ip',templet:function(row){
                                    // 使用正则表达式匹配 IP 地址
                                    const regex = /IP:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
                                    const matches = row.deviceId.match(regex);
                                    // 提取匹配到的 IP 地址
                                    const ipAddress = matches ? matches[1] : '';
                                    return ipAddress;
                                }},
                            {field: 'city', title:'城市'},
                            {field: 'region', title:'地区'},
                            {field: 'isp', title:'运营商'},
                            {field: 'eventName', title:'事件类型' },
                        ]],
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 15,
                        page: true,
                        skin: 'line'
                    });
                }
                reload();

                form.on('submit(data-search-btn)', function () {
                    reload();
                    return false;
                });

                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'export') {  // 监听添加操作
                        var searchData = form.val('table-search');
                        searchData.page = 1;
                        searchData.limit = 199999;
                        var layerIndex = layer.msg("请求中请耐心等待...",{icon:16,time:999999999});
                        $.get('/admin/roleLogin/export',searchData,function(res){
                            layer.close(layerIndex)
                            browserDownload("/" + res.data);
                        });
                    }
                });

            });
        }
    })

</script>
</body>
</html>